package cn.echo.modules.sys.controller.linkController;


import cn.echo.modules.sys.entity.Link;
import cn.echo.modules.sys.entity.SysAdmin;
import cn.echo.modules.sys.service.LinkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2017/4/14.
 */

@Controller
@RequestMapping("/link")
public class LinkController {

    @Resource
    private LinkService linkService;

    @RequestMapping(value = "show",method = RequestMethod.GET)
  public String show(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                     @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                     Model model){

        /*LinkService linkService = new LinkService();*/
        PageHelper.startPage(pageNum,pageSize);
        Page<Link> linkList = (Page) linkService.selectAll();

        PageInfo<Link> pageInfo = new PageInfo(linkList);
        model.addAttribute("page",pageInfo);
        return "sys/link/friendly_link";
    }


    @RequestMapping(value = "add0",method = RequestMethod.GET)
    public String add0(){
        return "sys/link/add_link";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(String linkName,String url,String linkWeight,String linkFlag){
        /*LinkService linkService = new LinkService();*/
        Link link = new Link();
        link.setId(UUID.randomUUID().toString());
        link.setName(linkName);
        link.setUrl(url);
        link.setWeight(Integer.parseInt(linkWeight));
        link.setDelFlag(linkFlag.charAt(0));
         SysAdmin sysAdmin= new SysAdmin();
        sysAdmin.setName("10086");
        link.setCreateBy(sysAdmin);
        link.setCreateDate(new Date());
        //创建人即更新人
        link.setUpdateBy(sysAdmin);
        link.setUpdateDate(new Date());
        boolean b = linkService.insert(link);
        //如果操作成功。，返回显示界面
        if(b){
            return "sys/link/friendly_link";
        }
        return "sys/link/error";
    }

    @RequestMapping(value = "update0",method = RequestMethod.GET)
    public String update0(String id,Model model){
        System.out.printf("id:::::::::::::::::::::"+id);
        model.addAttribute("id",id);
        return "sys/link/update_link";
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String update(String id ,String link_name,String link_url,String link_updateBy,String link_weight,String link_flag){
        System.out.println("要修改的id:"+id);
        Link link = new Link();
        link.setId(id);
        link.setName(link_name);
        link.setUrl(link_url);
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(link_updateBy);
        //更新人及创建人
        link.setUpdateBy(sysAdmin);
        link.setWeight(Integer.parseInt(link_weight));
        link.setDelFlag(link_flag.charAt(0));
        link.setCreateBy(sysAdmin);
        link.setCreateDate(new Date());
        link.setUpdateDate(new Date());
        /*LinkService linkService = new LinkService();*/
        boolean b = linkService.update(link);
        if(b){
            return "sys/link/friendly_link";
        }
        return "sys/link/error";
    }
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public String delete(String id){
        Link link = new Link();
        link.setId(id);
        /*LinkService linkService = new LinkService();*/
        boolean b = linkService.deleteByid(link);
        if(b){
            return "sys/link/friendly_link";
        }
        return "sys/link/error";
    }


    @RequestMapping(value = "",method =RequestMethod.GET)
    public String error(){
        return "sys/link/error";
    }
}
